package my.study.org.apache.ibatis.examples.example1.factory;

import java.io.IOException;
import java.io.Reader;

import my.base.util.FileUtil;
import my.study.org.apache.ibatis.examples.example1.Example1Main;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionFactory {
	private static SqlSessionFactory sqlSessionFactory;
	private static void init() {
		String resource = FileUtil.getPath(Example1Main.class) + "/Configuration.xml";
		Reader reader;
		try {
			reader = Resources.getResourceAsReader(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder()
			.build(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	public static SqlSessionFactory getInstance() {
		if (sqlSessionFactory == null) {
			synchronized (SessionFactory.class) {
				if (sqlSessionFactory == null) {
					init();
				}
			}
		} 
		return sqlSessionFactory;
		
	}

}
